package jnpf.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.service.SuperServiceImpl;
import jnpf.engine.enums.FlowTemplateRelationTypEnum;
import jnpf.entity.TemplateRelationEntity;
import jnpf.mapper.TemplateRelationMapper;
import jnpf.service.TemplateRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模板关系 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class TemplateRelationServiceImpl extends SuperServiceImpl<TemplateRelationMapper, TemplateRelationEntity> implements TemplateRelationService {

    @Override
    public Map<String, List<String>> getTypeRelations(FlowTemplateRelationTypEnum templateRelationTypEnum) {
        Map<String, List<String>> result = new HashMap<>();
        Map<String, List<TemplateRelationEntity>> map = list(Wrappers.lambdaQuery(TemplateRelationEntity.class)
                .eq(TemplateRelationEntity::getObjectType, templateRelationTypEnum.getType()))
                .stream()
                .collect(Collectors.groupingBy(TemplateRelationEntity::getObjectId));
        for (Map.Entry<String, List<TemplateRelationEntity>> entry : map.entrySet()) {
            result.put(entry.getKey(), entry.getValue().stream().map(TemplateRelationEntity::getOrganizeId).collect(Collectors.toList()));
        }
        return result;
    }

    @Override
    public void deleteByObjectId(FlowTemplateRelationTypEnum templateRelationTypEnum, String objectId) {
        baseMapper.delete(Wrappers.lambdaQuery(TemplateRelationEntity.class)
                .eq(TemplateRelationEntity::getObjectType, templateRelationTypEnum.getType())
                .eq(TemplateRelationEntity::getObjectId, objectId));
    }

    @Override
    public Boolean insertBatch(FlowTemplateRelationTypEnum templateRelationTypEnum, List<String> organizeIds, String objectId) {
        if (CollectionUtil.isEmpty(organizeIds)) {
            return false;
        }
        List<TemplateRelationEntity> addParam = new ArrayList<>();
        for (String organizeId : organizeIds) {
            TemplateRelationEntity entity = new TemplateRelationEntity();
            entity.setObjectId(objectId);
            entity.setOrganizeId(organizeId);
            entity.setObjectType(templateRelationTypEnum.getType());
            addParam.add(entity);
        }
        return saveBatch(addParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRelation(FlowTemplateRelationTypEnum templateRelationTypEnum, List<String> organizeIds, String objectId) {
        deleteByObjectId(templateRelationTypEnum, objectId);
        return insertBatch(templateRelationTypEnum, organizeIds, objectId);
    }

    @Override
    public List<String> getObjectIdsByOrganizeId(FlowTemplateRelationTypEnum templateRelationTypEnum, String organizeId) {
        return list(Wrappers.lambdaQuery(TemplateRelationEntity.class)
                .select(TemplateRelationEntity::getObjectId)
                .eq(TemplateRelationEntity::getObjectType, templateRelationTypEnum.getType())
                .eq(TemplateRelationEntity::getOrganizeId, organizeId))
                .stream()
                .map(TemplateRelationEntity::getObjectId)
                .collect(Collectors.toList());
    }
}